/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class Ellipse extends Shape {
    public Ellipse(int width, int height, java.awt.Color backColor, java.awt.Color borderColor, int borderThickness, boolean isFill) {
        super(width, height, backColor, borderColor, borderThickness, isFill);
    }
   
    private Couple getM() {
        if (dimension.x >= dimension.y)
            return getM(dimension.x, dimension.y);
        Couple m = getM(dimension.y, dimension.x);
        return new Couple(m.y, m.x);
    }
    
    private Couple getM(double w, double h) {
        double f2 = (w * w - h * h) / 4;
        double g2 = (1.0d - 4*f2/(w*w))*(h*h/4);
        return new Couple((int)(w/2 - java.lang.Math.sqrt(f2)), (int)(h/2 - java.lang.Math.sqrt(g2)));
    }
    
    @Override public List<Couple> getVertices() {
        if (!isVisible()) return emptyVertices;
        List<Couple> result = new ArrayList<>();
        Couple m =getM();
        result.add(new Couple(0, dimension.y / 2));
        result.add(new Couple(m.x, m.y));
        result.add(new Couple(m.x, dimension.y - m.y));
        result.add(new Couple(dimension.x / 2, 0));
        result.add(new Couple(dimension.x / 2, dimension.y));
        result.add(new Couple(dimension.x - m.x, m.y));
        result.add(new Couple(dimension.x - m.x, dimension.y - m.y));
        result.add(new Couple(dimension.x, dimension.y / 2));
        return result;
    }
    
    @Override public void paint(java.awt.Graphics2D g2d, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        g2d.setColor(getRealColor((backColor!=null)? backColor: style.shapeBackgroundColor, alpha));
        g2d.fillOval((int)r.x, (int)r.y, (int)dimension.x, (int)dimension.y);
        g2d.setColor(getRealColor((borderColor!=null)? borderColor: style.shapeBorderColor, alpha));
        g2d.setStroke(new java.awt.BasicStroke((borderThickness!=0)? borderThickness: style.shapeBorderThickness));
        g2d.drawOval((int)r.x, (int)r.y, (int)dimension.x, (int)dimension.y);
    }
    @Override protected void exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent, float alpha, DiagramStyle style, Couple offset) {
        if (!isVisible()) return;
        Couple r = position.plus(offset);
        org.w3c.dom.Element elem = doc.createElement("ellipse");
        elem.setAttribute("cx", Double.toString(r.x + dimension.x / 2));
        elem.setAttribute("cy", Double.toString(r.y + dimension.y / 2));
        elem.setAttribute("rx", Double.toString(dimension.x / 2));
        elem.setAttribute("ry", Double.toString(dimension.y / 2));
        java.awt.Color fill = getRealColor((backColor!=null)? backColor: style.shapeBackgroundColor, alpha);
        java.awt.Color border = getRealColor((borderColor!=null)? borderColor: style.shapeBorderColor, alpha);
        int stroke = (borderThickness!=0)? borderThickness: style.shapeBorderThickness;
        if (fill != null && fill.getAlpha() != 0) {
            elem.setAttribute("fill", "#" + getColorString(fill));
            if (fill.getAlpha() != 255) {
                float value = ((float)fill.getAlpha()) / 255;
                elem.setAttribute("fill-opacity", Float.toString(value));
            }
        }
        if (stroke > 0 && border != null && border.getAlpha() != 0) {
            elem.setAttribute("stroke", "#" + getColorString(border));
            if (border.getAlpha() != 255) {
                float value = ((float)border.getAlpha()) / 255;
                elem.setAttribute("stroke-opacity", Float.toString(value));
            }
            elem.setAttribute("stroke-width", Integer.toString(stroke));
        }
        parent.appendChild(elem);
    }
}
